package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbOrderItemMapper;
import com.dongdongshop.mapper.TbOrderMapper;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.pojo.TbOrderItemExample;
import com.dongdongshop.service.OrderItemService;
import com.dongdongshop.vo.CartVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * description 订单明细实现服务
 * author 刘雷雨
 * date 2022/9/10 15:59
 * version 1.0
 **/
@Service
@DubboService
public class OrderItemServiceImpl implements OrderItemService {

    @Autowired
    private TbOrderItemMapper orderItemMapper;
    @Autowired
    private TbOrderMapper orderMapper;

    //查询已支付的订单信息
//    @Override
//    public List<CartVO> findPaidOrder(String userName) {
//        List<CartVO> cartVOList = new ArrayList<CartVO>();
//        List<TbOrderItem> orderItems = orderItemMapper.findUserPaidOrder(userName);
//        for (TbOrderItem orderItem : orderItems) {
//            //当前集合为空
//            if (cartVOList.size() <= 0) {
//                //将第一条数据直接添加
//                List<TbOrderItem> orderItemList = new ArrayList<>();
//                orderItemList.add(orderItem);
//                cartVOList.add(new CartVO(orderItem.getSellerId(), orderItemList));
//            } else {
//                CartVO cartVO = getCartVOBySellerId(cartVOList, orderItem.getSellerId());
//                //不存在当前商家商品
//                if (cartVO == null) {
//                    List<TbOrderItem> orderItemList = new ArrayList<>();
//                    orderItemList.add(orderItem);
//                    cartVOList.add(new CartVO(orderItem.getSellerId(), orderItemList));
//                } else {
//                    List<TbOrderItem> orderItems1 = cartVO.getOrderItems();
//                    orderItems1.add(orderItem);
//                    cartVO.setOrderItems(orderItems1);
//                }
//            }
//        }
//        return cartVOList;
//    }
//
//    //根据商家ID获取商品信息
//    private CartVO getCartVOBySellerId(List<CartVO> cartVOList,String sellerId) {
//        for (CartVO cartVO : cartVOList) {
//            if (cartVO.getSellerId().equals(sellerId)) {
//                return cartVO;
//            }
//        }
//        return null;
//    }



    //根据支付订单号修改订单明细支付状态
    @Override
    public Integer updateOrderStatus(TbOrderItem orderItem) {
        return orderItemMapper.updateOrderStatus(orderItem);
    }

    //根据退款请求号修改订单明细支付状态
    @Override
    public Integer updateOrderStatus(String refundOrderNum) {

        //0 = 退款
        return  orderItemMapper.updateByPrimaryKeySelective(new TbOrderItem(Long.valueOf(refundOrderNum),"0"));
    }

    //按订单号和支付宝流水号和状态查找订单
    @Override
    public List<TbOrderItem> findOrderByOrderNoAndTradeNoAndStatus(String WIDTRout_trade_no, String widtRtrade_no) {
        TbOrderItemExample example = new TbOrderItemExample();
        example.createCriteria()
                .andOrderNumEqualTo(WIDTRout_trade_no)
                .andTradeNumEqualTo(widtRtrade_no)
                .andStatusNotEqualTo("0");//0 = 退款
        return orderItemMapper.selectByExample(example);
    }
}
